package ren.nearby.http.view;

import android.graphics.Color;
import android.view.View;

/**
 * Created by Administrator on 2019/3/13.
 */

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.support.annotation.Keep;
import android.support.annotation.Nullable;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;


import ren.nearby.http.R;

public class MyProgressBar extends View {
    private float ANG = 3.6F;
    private ObjectAnimator animator;
    private final int color;
    private final int mCount = 50;
    private OnProgressListener onProgressListener;
    private Paint paint;
    private int progress = 0;
    private final float radius = dpToPixel(150.0F);
    private final float sWith = dpToPixel(15.0F);

    public MyProgressBar(Context paramContext, @Nullable AttributeSet paramAttributeSet) {
        this(paramContext, paramAttributeSet, 0);
    }

    public MyProgressBar(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
        TypedArray localTypedArray = paramContext.obtainStyledAttributes(paramAttributeSet, R.styleable.ProcessView, paramInt, 0);
        this.color = localTypedArray.getDimensionPixelSize(0, getResources().getColor(R.color.ddd));
        int i = localTypedArray.getColor(R.styleable.ProcessView_pointer_color, 2000);
        localTypedArray.recycle();
        this.paint = new Paint(1);
        this.paint.setTextAlign(Paint.Align.CENTER);
        this.animator = ObjectAnimator.ofInt(this, "progress", new int[]{0, 100});
        this.animator.setDuration(i);
        this.animator.setInterpolator(new FastOutSlowInInterpolator());
    }

    private float dpToPixel(float paramFloat) {
        return paramFloat * Resources.getSystem().getDisplayMetrics().density;
    }

    public int getProgress() {
        return this.progress;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.animator.start();
    }
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.animator.end();
    }
    @Override
    protected void onDraw(Canvas paramCanvas) {
        super.onDraw(paramCanvas);
        float f1 = getWidth() >> 1;
        float f2 = getHeight();
        this.paint.setColor(this.color);
        this.paint.setStyle(Paint.Style.STROKE);
        this.paint.setStrokeCap(Paint.Cap.BUTT);
        this.paint.setStrokeWidth(dpToPixel(2.0F));
        paramCanvas.save();
        for (int i = 0; i < 51; i++) {
            if (i > this.progress / 2)
                this.paint.setColor(-1);
            paramCanvas.drawLine(f1 - this.radius, f2, f1 - this.radius + this.sWith, f2, this.paint);
            paramCanvas.rotate(this.ANG, f1, f2);
        }
        paramCanvas.restore();
        paramCanvas.save();
        paramCanvas.rotate(1.8F * this.progress, f1, f2);
    }

    @Keep
    public void setProgress(int paramInt) {
        this.progress = paramInt;
        if (this.onProgressListener != null)
            this.onProgressListener.onProgress(paramInt);
        invalidate();
    }

    public void setProgressListener(OnProgressListener paramOnProgressListener) {
        this.onProgressListener = paramOnProgressListener;
    }

    public static abstract interface OnProgressListener {
        public abstract void onProgress(int paramInt);
    }
}
